package org.erp.store.infrastructure.util;

/**
 * 幂等键生成工具
 */
public final class IdempotencyKeyUtil {

    private IdempotencyKeyUtil() {}

    /**
     * 批量状态更新幂等键：batch_status_update:inventoryId:operateId:timestamp
     */
    public static String buildStatusUpdateKey(String productCode, Long operateId) {
        return "batch_status_update:" + productCode + ":" + operateId + ":" + System.currentTimeMillis();
    }

    /**
     * 批量入库幂等键：
     * - 若给定请求幂等键：batch_storage:requestKey:inventoryId
     * - 否则：batch_storage:inventoryId:operateIdOrZero:timestamp
     */
    public static String buildBatchStorageKey(String productCode, Long operateId, String requestIdempotencyKey) {
        if (requestIdempotencyKey != null && !requestIdempotencyKey.isEmpty()) {
            return "batch_storage:" + requestIdempotencyKey + ":" + productCode;
        }
        long opId = operateId != null ? operateId : 0L;
        return "batch_storage:" + productCode + ":" + opId + ":" + System.currentTimeMillis();
    }
}
